import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:basic/basic.dart';
import 'package:equatable/equatable.dart';

import 'constant.dart';

class AppConfig {
  static bool isRelease = false;
  static Map<String, dynamic>? appConfig;

  ///app信息
  static late int appId; //appId
  static late String appName; //app名称
  static late String applicationId; //applicationId
  static late String packageName; //包名
  static String? appLogoAssets; //applogo assets
  static String? appLogoPath; //applogo 网络地址
  static String? versionName; //版本号  1.0.0
  static String? versionCode; //版本号  100

  //项目配置
  static late ProjectConfig project;

  ///环境配置
  static List<NetConfig>? netsConfig; //网络配置
  static NetConfig? appNetConfig; //当前app环境

  static String baseUrl = ''; //base url
  static String extendUrl = ''; //扩展url
  static int siteId = 0; //站点
  static String apiUrl = ''; //api地址
  static String h5Url = ''; //h5地址
  static String imageUrl = ''; //图片地址

  static String? deviceId = ''; //设备id
  static BuglyParams? bugly; //bugly参数
  static AppShareParams? share; //分享参数
  static GeTuiParams? geTui; //个推参数

  ///网络配置
  static ConnectivityResult connectivityResult = ConnectivityResult.wifi;

  ///项目初始化
  static init(Map<String, dynamic> json) async {
    appConfig = json;
    isRelease = json['isRelease'];
    appId = json['appId'];
    appName = json['appName'];
    applicationId = json['applicationId'];
    packageName = json['packageName'];
    appLogoAssets = json['appLogoAssets'];
    appLogoPath = json['appLogoPath'];
    versionCode = await DeviceUtil.buildNumber();
    versionName = await DeviceUtil.version();

    _initProjectConfig(json['projectConfig']); //初始化项目配置
    initNetsConfig(json["netsConfig"]); //初始化net配置
    initAppNetConfig();

    _initBuglySDK(json['buglySDK']);
    _initShareSDK(json['shareSDK']);
    if (!isRelease) {
      //发布版本不打印
      LogUtil.v(
        "app名称 :$appName  applicationId :$applicationId  versionName :$versionName  versionCode :$versionCode  英文简称 :$packageName  站点 :$siteId  当前环境 :${appNetConfig?.explain ?? ''}  ",
      );
    }
  }

  ///初始化项目配置
  static void _initProjectConfig(
    Map<String, dynamic>? projectConfigParams,
  ) async {
    project = ProjectConfig();
  }

  //初始化net配置
  static initNetsConfig(List? netsConfigData) {
    netsConfig = (netsConfigData ?? []).map((e) {
      return NetConfig.fromJson(e);
    }).toList();
  }

  // 获取当前App 的网络配置
  static NetConfig? getAppNetConfig() {
    if (netsConfig?.isEmpty ?? true) return null;

    NetConfig firstAppNetConfig = netsConfig!.first;
    if (AppConfig.isRelease) {
      StorageManager.localStorage.removeItem(
        AppLocalStorageKey.appNetConfig,
      ); //清除本地环境配置
      return firstAppNetConfig;
    }

    dynamic localAppNetConfigData = StorageManager.localStorage.getItem(
      AppLocalStorageKey.appNetConfig,
    );

    if (localAppNetConfigData != null &&
        localAppNetConfigData is Map<String, dynamic>) {
      return NetConfig.fromJson(localAppNetConfigData);
    }
    return firstAppNetConfig;
  }

  //设置网络
  static initAppNetConfig() {
    if (netsConfig?.isEmpty ?? true) return;

    appNetConfig = getAppNetConfig();
    if (appNetConfig != null) {
      baseUrl = appNetConfig?.url ?? '';
      extendUrl = appNetConfig?.extendUrl ?? '';

      apiUrl = '$baseUrl/';
      imageUrl = '$baseUrl/';
      siteId = appNetConfig?.siteId ?? 0;
      h5Url = '$baseUrl/';
    }
  }

  ///初始化腾讯bugly
  static Future _initBuglySDK(Map<String, dynamic>? buglyParams) async {
    if (buglyParams != null) {
      bugly = BuglyParams.fromJson(buglyParams);
      LogUtil.v(
        " buglyParams iosAppId=${buglyParams['iosAppId']}   androidAppId=${buglyParams['androidAppId']} --->>> ",
      );
    }
  }

  ///初始化分享 QQ 微信
  static Future<void> _initShareSDK(Map<String, dynamic>? shareParams) async {
    if (shareParams?.isEmpty ?? true) return;
    share = AppShareParams.fromJson(shareParams ?? {}); //分享
  }

  //重新设置
  static reSetAppNetConfig() async {
    initAppNetConfig();
  }
}

const List<String> defaultFileFormats = ["epub", "pdf"];

//项目配置
class ProjectConfig {}

//网络配置
class NetConfig extends Equatable {
  final int key;
  final String? url;
  final String? extendUrl; //扩展url
  final String? websiteUrl; //网站url
  final String? h5Path;
  final int? siteId;
  final String? explain; //简介

  const NetConfig(
    this.key,
    this.url,
    this.extendUrl,
    this.websiteUrl,
    this.h5Path,
    this.siteId,
    this.explain,
  );

  NetConfig.fromJson(Map<String, dynamic> json)
    : key = json['key'],
      url = json['url'],
      extendUrl = json['extendUrl'],
      websiteUrl = json['websiteUrl'],
      h5Path = json['h5Path'],
      siteId = json['siteId'],
      explain = json['explain'];

  Map<String, dynamic> toJson() => {
    'key': key,
    'url': url,
    'extendUrl': extendUrl,
    'websiteUrl': websiteUrl,
    'h5Path': h5Path,
    'siteId': siteId,
    'explain': explain,
  };

  @override
  List<Object?> get props => [key];
}

///bugly 参数
class BuglyParams {
  String? iosAppId;
  String? androidAppId;

  BuglyParams({this.iosAppId, this.androidAppId});

  factory BuglyParams.fromJson(Map<String, dynamic> json) {
    return BuglyParams(
      iosAppId: json['iosAppId'],
      androidAppId: json['androidAppId'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['iosAppId'] = iosAppId;
    data['androidAppId'] = androidAppId;
    return data;
  }
}

///tts 参数
class TtsParams {
  String appId;

  TtsParams({required this.appId});

  factory TtsParams.fromJson(Map<String, dynamic> json) {
    return TtsParams(appId: json['appId']);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['appId'] = appId;
    return data;
  }
}

///分享参数
class AppShareParams {
  final String? appKey;
  final String? appSecret;
  final String? logoPath; //icon地址
  final String? defaultTitle; //默认title
  final String? defaultText; //默认文本
  Map<String, dynamic>? qqParams;
  Map<String, dynamic>? wechatParams;
  Map<String, dynamic>? dingParams;
  Map<String, dynamic>? sinaParams;

  AppShareParams({
    this.appKey,
    this.appSecret,
    this.logoPath,
    this.defaultTitle,
    this.defaultText,
    this.qqParams,
    this.wechatParams,
    this.dingParams,
    this.sinaParams,
  });

  factory AppShareParams.fromJson(Map<String, dynamic> json) {
    return AppShareParams(
      appKey: json['appKey'],
      appSecret: json['appSecret'],
      logoPath: json['logoPath'],
      defaultTitle: json['defaultTitle'],
      defaultText: json['defaultText'],
      qqParams: json['qq'],
      wechatParams: json['wechat'],
      dingParams: json['ding'],
      sinaParams: json['sina'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['appKey'] = appKey;
    data['appSecret'] = appSecret;
    data['logoPath'] = logoPath;
    data['defaultTitle'] = defaultTitle;
    data['defaultText'] = defaultText;
    data['qq'] = qqParams;
    data['wechat'] = wechatParams;
    data['ding'] = dingParams;
    data['sina'] = sinaParams;
    return data;
  }
}

///个推参数
class GeTuiParams {
  String appId;
  String appKey;
  String appSecret;

  GeTuiParams({this.appId = '', this.appKey = '', this.appSecret = ''});

  factory GeTuiParams.fromJson(Map<String, dynamic> json) {
    return GeTuiParams(
      appId: json['appId'],
      appKey: json['appKey'],
      appSecret: json['appSecret'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['appId'] = appId;
    data['appKey'] = appKey;
    data['appSecret'] = appSecret;
    return data;
  }
}
